package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.Result;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.vo.ItemCatVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("cat")
public class ItemCatController {

    @Autowired
    private ItemCatService is;

    @PostMapping("catListByParentId")
    public Result catListByParentId(Long parentId){
        List<ItemCatVO> parentIdList = is.catListByParentId(parentId);
        return Result.OK().setData(parentIdList);
    }

    @PostMapping("toUpdate")
    public Result toUpdate(Long id){
        ItemCatVO catVO = is.tuUpdate(id);
        return Result.OK().setData(catVO);
    }

    @PostMapping("update")
    public Result update(@RequestBody ItemCatVO catVO){
//        ItemCatVO itemCatVO = JSONObject.parseObject(catVO,ItemCatVO.class);
        is.update(catVO);
        return Result.OK();
    }

    @PostMapping("add")
    public Result add(@RequestBody ItemCatVO catVO){
        is.add(catVO);
        return Result.OK();
    }

    @PostMapping("delete")
    public Result delete(Long id){
        is.delete(id);
        return Result.OK();
    }

}
